package com.example.warehouse.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.warehouse.common.QueryPageParam;
import com.example.warehouse.common.Result;
import com.example.warehouse.entity.Goods;
import com.example.warehouse.entity.Record;
import com.example.warehouse.service.GoodsService;
import com.example.warehouse.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 小冯
 * @since 2023-01-18
 */
@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private RecordService recordService;
    @Autowired
    private GoodsService goodsService;

    @PostMapping("/listPageC1")
    public Result listPageC1(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String) param.get("name");
        String storage = (String) param.get("storage");
        String goodstype = (String) param.get("goodstype");
        String roleId = (String) param.get("roleId");
        String userId = (String) param.get("userId");

        Page<Record> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        QueryWrapper<Record> queryWrapper = new QueryWrapper();
        queryWrapper.apply(" a.goods=b.id and b.storage=c.id  and b.goodstype = d.id");
        if("2".equals(roleId)){
            queryWrapper.apply("a.userId="+userId);
        }
        if (StringUtils.isNotBlank(name)&&!"null".equals(name)){
            queryWrapper.like("b.name",name);
        }
        if (StringUtils.isNotBlank(storage)&&!"null".equals(storage)){
            queryWrapper.eq("c.id" ,storage);
        }
        if (StringUtils.isNotBlank(goodstype)&&!"null".equals(goodstype)){
            queryWrapper.eq("d.id",goodstype);
        }
        IPage result =recordService.pageCC(page,queryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }

    //出入库
    @PostMapping("/save")
    public Result save(@RequestBody Record record){
        Goods goods = goodsService.getById(record.getGoods());
        int newNum = 0;
        if (record.getOutorin().equals("1"))
            newNum=goods.getCount()+record.getCount();
        else
            newNum=goods.getCount()-record.getCount();
        goods.setCount(newNum);
        goodsService.updateById(goods);
        // DateTimeFormatter.ofPattern方法根据指定的格式输出时间
        LocalDateTime formatDateTime = LocalDateTime.now();
        record.setCreatetime(formatDateTime);
        return recordService.save(record)?Result.suc():Result.fail();
    }
}
